package top.huntwolf.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.pojo.Subject;
import top.huntwolf.admin.service.SubjectService;

import java.util.List;

@RestController
@RequestMapping("/cms/subject")
@CrossOrigin
@Api(description = "cms书本分类控制器")
public class CMSSubjectController {

    @Autowired
    SubjectService subjectService;

    @PostMapping("/excel")
    @ApiOperation(value = "Excel批量导入")
    public JsonResult addSubject(MultipartFile file) {
        System.out.println(subjectService);
        subjectService.addSubjectByExcel(file, subjectService);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @RequiresPermissions(value = "subject:find")
    @GetMapping("")
    @ApiOperation("获取所有分类")
    public JsonResult getList() {
        List list = subjectService.getList();
        return JsonResult.success(list);
    }

    @RequiresPermissions(value = "subject:modify")
    @PostMapping("updateSubject")
    @ApiOperation("对分类的名称、从属关系进行更改")
    public JsonResult update(@RequestBody Subject subject){
        subjectService.update(subject);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @RequiresPermissions(value = "subject:remove")
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除分类")
    public JsonResult deleteSubject(@PathVariable int id){
        subjectService.deleteSubject(id);
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "subject:add")
    @PostMapping("")
    @ApiOperation("增加分类")
    public JsonResult addSubject(@RequestBody Subject subject){
        subjectService.save(subject);
        return JsonResult.success(null);
    }

    // ----------------------------------------------------- 以下为热搜功能 ----------------------------------------------
    @RequiresPermissions(value = "subject:remove")
    @DeleteMapping("hotSort/{subjectId}")
    @ApiOperation(value = "删除指定热门搜索")
    public JsonResult removeHotSort(@PathVariable Integer subjectId) {
        subjectService.removeHotSort(subjectId);
        return JsonResult.success(CodeMsg.SUCCESS);
    }
    @RequiresPermissions(value = "subject:modify")
    @PostMapping("hotSort")
    @ApiOperation(value = "指定热门搜索，在数据量还没那么多的时候，由管理员后台指定")
    public JsonResult addHotSort(@RequestBody List<Subject> list) {
        subjectService.addHotSort(list);
        return JsonResult.success(CodeMsg.SUCCESS);
    }
    @RequiresPermissions(value = "subject:modify")
    @PutMapping("hotSort")
    @ApiOperation(value = "管理员可以批量手动修改热门搜索的多少,前台设置只允许修改sort，避免出现把分类id也修改")
    public JsonResult updateHotSorts(@RequestBody List<Subject> list) {
        subjectService.updateHotSort(list);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

}
